// pages2/pages/mall/healthPoints/index.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"
const $api = require("../../../../request/api").API
import {judgeAuth,openAuth} from "../../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    describes:"",
    healthState:"",
    showOverlay:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.recordRecognitionManager = requirePlugin('WechatSI').getRecordRecognitionManager();
    let record = await judgeAuth('scope.record')  
    if(record===undefined){
      wx.authorize({
        scope: 'scope.record',
        success:()=>{
         this.recordSoundAuth = true
        }
      })
    }else if(!record){
      await openAuth(scope.record);
      this.recordSoundAuth = true
    }else{
      this.recordSoundAuth = true
    }
    this.initRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  //识别语音 -- 初始化
  initRecord: function () {
    // 有新的识别内容返回，则会调用此事件
    this.recordRecognitionManager.onRecognize = (res)=> {
    }
    // 正常开始录音识别时会调用此事件
    this.recordRecognitionManager.onStart =  (res)=>{
      this.duringRecord = true // 把值设置为true,表示不是进来的时候识别录音
      wx.vibrateLong()
      this.setData({
        showOverlay:true
      })
    }
    // 识别错误事件
    this.recordRecognitionManager.onError =  (res)=>{
      console.error("error msg", res)
      Toast.clear()
      this.startRecord = false
      let msg = "语音报错"
      if(res.retcode=="-30011"){
        msg="录制太频繁"
      }else if(!this.duringRecord){
        // 如果this.duringRecord不存在表示进来的还在识别就直接不报错了
        return
      }
      Toast.fail(msg)
    }
    //识别结束事件
    this.recordRecognitionManager.onStop =  (res)=> {
      Toast.clear()
      this.startRecord = false
      if(!this.duringRecord) return // 进来的时候还在识别，那么即使是被成功也不需要了
      console.log('..............结束录音')
      console.log('录音临时文件地址 -->' + res.tempFilePath); 
      console.log('录音总时长 -->' + res.duration + 'ms'); 
      console.log('文件大小 --> ' + res.fileSize + 'B');
      console.log('语音内容 --> ' + res.result);
      this.setData({
        describes:res.result,
      })
      if (res.result == '') {
        wx.showModal({
          title: '提示',
          content: '听不清楚，请重新说一遍！',
          showCancel: false,
          success: function (res) {}
        })
        return;
      }
    }
  },
  hideOverlay(){
    this.setData({
      showOverlay:false
    })
  },
  recordSound(){
    console.log("开始")
      // this.selectComponent("#sound-record-two").start()
      if(!this.recordSoundAuth) return
      this.startRecord = true
      console.log("开始")
      this.recordRecognitionManager.start({
        lang: 'zh_CN',
        duration:10000
      })
  },
  endSound(){
  
    if(!this.recordSoundAuth) return
    this.setData({
      showOverlay:false,
    })
    if(this.startRecord){
      Toast.loading({
        message:"语音识别中",
        forbidClick:true,
        mask:true,
        duration:0
      })
      this.recordRecognitionManager.stop();
    }
  },
  handleInput(e){
    this.setData({
      describes:e.detail.value
    })
  },
  handleSelect(e){
    const {state} = e.currentTarget.dataset
    this.setData({
      healthState:state,
      describes:"",
    })
    if(state===1||state===2){
      this.submit()
    }
  },
  async submit(){
    Toast.loading({
      duration:0,
      forbidClick:true,
      message:"请稍等"
    })
    const {healthState,describes} = this.data
    let res = await $api.healthClock({healthState,describes})
    res = res.data.data==2?'打卡成功积分加10':'打卡成功'
    Toast.success(res)
    

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})